package com.papercutting.platform.controller.admin;

import com.papercutting.platform.common.Result;
import com.papercutting.platform.dto.request.TutorialStepRequest;
import com.papercutting.platform.dto.response.TutorialStepResponse;
import com.papercutting.platform.service.TutorialStepService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 管理员-教程步骤控制器
 */
@Slf4j
@RestController("adminTutorialStepController")
@RequestMapping("/api/admin/tutorial-steps")
@RequiredArgsConstructor
@Validated
public class TutorialStepController {
    
    private final TutorialStepService tutorialStepService;
    
    /**
     * 根据教程ID获取步骤列表
     */
    @GetMapping("/tutorial/{tutorialId}")
    public Result<List<TutorialStepResponse>> getTutorialSteps(@PathVariable @NotNull Long tutorialId) {
        log.info("获取教程步骤列表: {}", tutorialId);
        
        List<TutorialStepResponse> steps = tutorialStepService.getTutorialSteps(tutorialId);
        
        return Result.success(steps);
    }
    
    /**
     * 获取步骤详情
     */
    @GetMapping("/{id}")
    public Result<TutorialStepResponse> getTutorialStepDetail(@PathVariable @NotNull Long id) {
        log.info("获取教程步骤详情: {}", id);
        
        TutorialStepResponse step = tutorialStepService.getTutorialStepById(id);
        
        return Result.success(step);
    }
    
    /**
     * 创建教程步骤
     */
    @PostMapping
    public Result<Long> createTutorialStep(@Valid @RequestBody TutorialStepRequest request) {
        log.info("创建教程步骤: {}", request.getTitle());
        
        Long stepId = tutorialStepService.createTutorialStep(request);
        
        return Result.success("步骤创建成功", stepId);
    }
    
    /**
     * 更新教程步骤
     */
    @PutMapping("/{id}")
    public Result<Void> updateTutorialStep(
            @PathVariable @NotNull Long id,
            @Valid @RequestBody TutorialStepRequest request) {
        log.info("更新教程步骤: {}", id);
        
        request.setId(id);
        tutorialStepService.updateTutorialStep(request);
        
        return Result.success("步骤更新成功");
    }
    
    /**
     * 删除教程步骤
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteTutorialStep(@PathVariable @NotNull Long id) {
        log.info("删除教程步骤: {}", id);
        
        tutorialStepService.deleteTutorialStep(id);
        
        return Result.success("步骤删除成功");
    }
    
    /**
     * 批量创建教程步骤
     */
    @PostMapping("/batch")
    public Result<Void> batchCreateTutorialSteps(@Valid @RequestBody List<TutorialStepRequest> requests) {
        log.info("批量创建教程步骤: {} 个", requests.size());
        
        tutorialStepService.batchCreateTutorialSteps(requests);
        
        return Result.success("批量创建成功");
    }
    
    /**
     * 重新排序教程步骤
     */
    @PutMapping("/tutorial/{tutorialId}/reorder")
    public Result<Void> reorderTutorialSteps(
            @PathVariable @NotNull Long tutorialId,
            @RequestBody List<Long> stepIds) {
        log.info("重新排序教程步骤: {}", tutorialId);
        
        tutorialStepService.reorderTutorialSteps(tutorialId, stepIds);
        
        return Result.success("排序更新成功");
    }
}
